namespace ShapesLib
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Text;

    /// <summary>
    /// Class to draw ellipses.
    /// </summary>
    public class Ellipse : Shape
    {
        #region Members
        /// <summary>
        /// Radius 1
        /// </summary>
        private int r1;

        /// <summary>
        /// Radius 2
        /// </summary>
        private int r2;
        #endregion

        #region constructor(s)

        /// <summary>
        /// Initializes a new instance of the <see cref="Ellipse"/> class.
        /// </summary>
        /// <param name="x">The x.</param>
        /// <param name="y">The y.</param>
        /// <param name="r1">The r1.</param>
        /// <param name="r2">The r2.</param>
        public Ellipse(int x, int y, int r1, int r2)
        {
            X = x;
            Y = y;
            this.R1 = r1;
            this.R2 = r2;
            FillColor = Color.Green;
        }
        #endregion

        #region Properities
        /// <summary>
        /// Gets or sets the r1.
        /// </summary>
        /// <value>
        /// The r1.
        /// </value>
        public int R1
        {
            get { return this.r1; }
            set { this.r1 = value; }
        }

        /// <summary>
        /// Gets or sets the r2.
        /// </summary>
        /// <value>
        /// The r2.
        /// </value>
        public int R2
        {
            get { return this.r2; }
            set { this.r2 = value; }
        }
        #endregion

        #region Methods
        /// <summary>
        /// Draws the specified g.
        /// </summary>
        /// <param name="g">The g.</param>
        public override void Draw(Graphics g)
        {
            SolidBrush solidBrush = new SolidBrush(FillColor);
            g.FillEllipse(solidBrush, X, Y, this.R1, this.R2);

            Pen myPen = new Pen(PenColor, PenWidth);
            g.DrawEllipse(myPen, X, Y, this.R1, this.R2);
        }

        /// <summary>
        /// Scales the specified factor.
        /// </summary>
        /// <param name="factor">The factor.</param>
        public override void Scale(double factor)
        {
            this.R1 = (int)((double)this.R1 * factor);
            this.R2 = (int)((double)this.R2 * factor);
        }

        #region Section2 methods
        /// <summary>
        /// Checks if point is in shape.
        /// </summary>
        /// <param name="x">The x.</param>
        /// <param name="y">The y.</param>
        /// <returns>Point in the shape.</returns>
        public override bool PointInShape(int x, int y)
        {
            int radius1 = this.R1 / 2;
            int radius2 = this.R2 / 2;
            int centerX = X + radius1;
            int centerY = Y + radius2;
            if ((double)((Math.Pow(radius2, 2) * Math.Pow(x - centerX, 2)) + (Math.Pow(radius1, 2) * Math.Pow(y - centerY, 2))) < (double)(Math.Pow(radius1, 2) * Math.Pow(radius2, 2)))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion

        #region Section3 method
        /// <summary>
        /// Draws for creation.
        /// </summary>
        /// <param name="g">The g.</param>
        public override void DrawForCreation(Graphics g)
        {
            SolidBrush solidBrush = new SolidBrush(Color.FromArgb(128, FillColor));
            g.FillEllipse(solidBrush, X, Y, this.R1, this.R2);

            Pen myPen = new Pen(PenColor, PenWidth);
            myPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
            g.DrawEllipse(myPen, X, Y, this.R1, this.R2);
        }

        #endregion

        #endregion
    }
}
